import BaseService from "./BaseService.js";
const app = getApp();
const util = require('../util');

module.exports = {
  //断电
  instructSendOff:function (data,port) {
    return BaseService.commonIot("instruct"+port+"/instructSendOff", data);
  },

  //通电
  instructSendNo:function (data,port) {
    return BaseService.commonIot("instruct"+port+"/instructSendNo", data);
  },

  //物联网设备检测（未用）
  selfCheck:function (data) {
    return BaseService.common("selfCheck/selfCheck", data);
  },

  //查询
  findList: function (data,otherDataAuth,isConnect) {
    let currentUser=app.globalData.userInfo;
    if(currentUser){
      if(isConnect){
        data.userDataAuthority = currentUser.dataAuthorityOfUser;
        currentUser.dataAuthorityOfUser.depart=currentUser.installDepart;
        util.uniqueConcat(data.userDataAuthority.depart,otherDataAuth);
      }else{
        let isAll = false;

        //查询条件中不验证权限
        if(data.checkDataAuthority==false){
          isAll = true;
        }else{
          currentUser.roles.forEach(role => {
            if(role.appId.indexOf(3)!=-1 && role.dataAuthOfRole.key == 'all') {
              isAll = true;
            }
          })
        }

        if(isAll){
          data.checkDataAuthority = false;
        }else{
          data.userDataAuthority = currentUser.dataAuthorityOfUser;
          if (otherDataAuth) {
            util.uniqueConcat(data.userDataAuthority.depart,otherDataAuth);
          }
        }
      }

      let checkState= {
        "fieldName": "validState",
        "relation": "!=",
        "value": 0
      };
      if(!data.condition){
        data.condition=[];
      }
      data.condition.push(
        checkState
      );
    }
    return BaseService.nb("findList", data);
  },

  //查询异常数据
  findErrorDataListGo:function(data){
    return BaseService.go("errorData/findList", data);
  },

  //查询异常数据-内部人员
  findErrorDataInsideListGo:function(data){
    return BaseService.go("errorData/findListInside", data);
  },

  //作废异常数据
  deleteErrorDataListGo:function(data){
    return BaseService.go("errorData/deleteErrorData", data);
  },

  //添加/修改
  save:function (data) {
    return BaseService.nb("save", data);
  },

  //修改单一字段
  update:function (data) {
    return BaseService.nb("update", data);
  },

  //删除
  delete:function (data) {
    return BaseService.nb("delete", data);
  },

  //旧模块的指令
  sendcmd:function (data) {
    return BaseService.equipmentIot("sendcmd", data);
  },

  //获取用户信息
  getUserByName:function (data) {
    return BaseService.authority("user/getUserByName", data);
  },

  //获取用户信息（用于验证码登录前的判断，不进行token验证）
  getUserName:function (data) {
    return BaseService.authority2("user/getUserName", data);
  },

  //获取个人中心菜单
  getMenu:function (data) {
    return BaseService.authority("menu/findList", data);
  },

  //发送验证码
  sendSms:function (data) {
    return BaseService.wechat2("sendSms/send", data);
  },

  //查询验证码
  querySms:function (data) {
    return BaseService.wechat2("sendSms/querySms", data);
  },

  //公众号授权
  wechatLogin:function (data) {
    return BaseService.wechat("login/wechatLogin", data);
  },

  //添加报修
  repairAdd:function (data) {
    return BaseService.equipment2("repair/saveOrUpdate", data);
  },

  //公众号发送维修提醒
  sendRepairTiXing:function (data) {
    return BaseService.wechat2("sendRepairTiXing/sendRepairTiXing", data);
  },

  //绑定kafka分区？？？
  bindPartition:function (data) {
    return BaseService.common("partition/bindPartition", data);
  },

  //外电补偿
  powerCompensate:function (data,port) {
    return BaseService.commonIot("instruct"+port+"/powerCompensate", data);
  },
  //设置震动模式
  setVibration:function (data,port) {
    return BaseService.commonIot("instruct"+port+"/setVibration", data);
  },
  //设置硬件模式
  recoveryInit:function (data,port) {
    return BaseService.commonIot("instruct"+port+"/recoveryInit", data);
  },
  //指令-设置4G模块开启外电检测功能
  openPowerCheck:function (data,port) {
    return BaseService.commonIot("instruct"+port+"/openPowerCheck4G", data);
  },
  //指令-打开静止过滤
  staticFilterON:function (data,port) {
    return BaseService.commonIot("instruct"+port+"/staticFilterON", data);
  },
  //指令-关闭静止过滤
  staticFilterOFF:function (data,port) {
    return BaseService.commonIot("instruct"+port+"/staticFilterOFF", data);
  },

  //月度报表-类别统计
  runInformation:function (data) {
    return BaseService.report("equipment/runInformation", data);
  },

  //月度报表-数据分析图表？？？（未用）
  usageInformation:function (data) {
    return BaseService.report("equipment/usageInformation", data);
  },

  //调用用友接口，自动生成采购入库单
  yyInterfaces:function(data){
    return BaseService.yy("bills/eInStorage", data);
  },

  //设备发货生成配件采购单
  automaticSaveDeliveryPartsRecord:function(data){
    return BaseService.go("deliveryParts/automaticSaveDeliveryPartsRecord", data);
  },

  //考勤统计-团队-日统计
  dayTeam:function (data) {
    return BaseService.common("personnel/selectAttendanceStatisticsDayTeam", data);
  },

  //考勤统计-团队-月统计
  monthTeam:function (data) {
    return BaseService.common("personnel/selectAttendanceStatisticsMonthTeam", data);
  },

  //考勤统计-各部门出勤率
  attendanceRate:function (data) {
    return BaseService.common("personnel/selectDepartAttendanceRate", data);
  },

  //报表统计-周排行
  recordOfWeek:function (data) {
    return BaseService.report("equipment/getClientEquipmentRunDataRecordOfWeekInWeChat", data);
  },

  //报表统计-周统计
  reportByWeek:function (data) {
    return BaseService.go("chart/weeklyStatistics", data);
  },

  //指令-设备进场
  deleteIotRunData:function (data) {
    return BaseService.goDataOprIot("deleteIotRunData", data);
  },

  //go项目-查询设备列表
  goFindEquipmentList: function (data,isAuth) {
    //是否按权限查询
    let isAuthFlag=true;
    if(isAuth===false){
      isAuthFlag=false;
    }

    let currentUser=app.globalData.userInfo;
    if(currentUser){
      let dataAuthOfRoles=currentUser.roles;
      let isAll=false;
      dataAuthOfRoles.forEach(role=>{
        for(let key in role.dataAuthOfRole){
          if(role.dataAuthOfRole[key]=='all'){
            isAll=true;
          }
        }
      })
      if(currentUser && (!isAll) && isAuthFlag){
        data.departAuth=currentUser.dataAuthorityOfUser.depart;
      }
    }
    return BaseService.goEquipment("findList", data);
  },

  //go项目-维修提醒
  sendEquipmentRepair:function (data){
    return BaseService.go("wechat/sendEquipmentRepair", data);
  },

  //go项目-通用查询
  goFindList:function (param){
    /*参数传入一个对象，key固定
      {
        isAuth:是否按权限查询,
        className:查哪个表,
        data:{}查询的字段，也就是实际参数
      }
    */

    if(param.isAuth===true){
      //获取用户权限
      let currentUser=app.globalData.userInfo;
      if(currentUser){
        let dataAuthOfRoles=currentUser.roles;
        let isAll=false;
        dataAuthOfRoles.forEach(role=>{
          for(let key in role.dataAuthOfRole){
            if(role.dataAuthOfRole[key]=='all'){
              isAll=true;
            }
          }
        })
        if(currentUser && (!isAll)){
          param.data.departAuth=currentUser.dataAuthorityOfUser.depart;
        }
      }
    }

    //日志（传入页面路径）
    let pages = getCurrentPages();
    let lastPage = pages[pages.length - 1];
    param.data.function = lastPage.route;

    return BaseService.goNb(param.className+"/findList", param.data);
  },

  //go项目-通用保存
  goSave:function (param){
    // 传参说明:
    // let param={
    //   className:表名（名称是后台配置文件写好的）
    //   data:{}字段
    // }

    //日志（传入页面路径）
    let pages = getCurrentPages();
    let lastPage = pages[pages.length - 1];
    param.data.function = lastPage.route;

    return BaseService.goNb(param.className+"/save", param.data);
  },

  //go项目-关联查询字段更新到设备表
  updateEquipmentAll:function (data){
    return BaseService.goDataOpr("updateEquipmentAll", data);
  },

  //同步到es
  mongoToElastic:function(data){
    return BaseService.goDataOpr("mongoToElastic", data);
  },

  //检修工单配件保存到配件更换明细
  closeOverhaulOrder(data){
    return BaseService.go("es/afterSaleRepair/closeOverhaulOrder", data);
  },

  //删除es中数据
  deleteToElastic:function(data){
    return BaseService.goDataOpr("deleteEs", data);
  },

  //go项目-设备分组
  getGroup:function (data){
    return BaseService.goUI("equipmentFast/getGroup", data);
  },

  //go项目-个人中心-简报看板
  personCenterCounts:function (data){
    return BaseService.goUI("personCenterCounts",data);
  },

  //go项目-删除
  goDelete:function (param){
    return BaseService.goNb(param.className+"/delete", param.data);
  },

  //电子工牌-删除es中数据
  goDeleteToEs:function (param){
    return BaseService.goCard("delete", param.data);
  },

  //go项目-更新
  goUpdate:function (param){
    return BaseService.goNb(param.className+"/update", param.data);
  },

  //go项目-查询物联网信息-单台设备
  goFindOneByImei:function (param){
    return BaseService.goEquipment("findOneByImei", param);
  },

  //go项目-物联网数据运行统计
  getRunTimeByDateRange:function (data){
    return BaseService.goDataReport("getRunTimeByDateRange", data);
  },

  //go项目-物联网日志
  iotDatalog:function (param){
    return BaseService.go("log/iotDatalog", param);
  },

  //查询异常数据阈值
  getThreshold:function (param){
    return BaseService.goEquipment("getRunTimeThreshold", param);
  },

  //设置异常数据阈值
  setThreshold:function (param){
    return BaseService.goEquipment("setRunTimeThreshold", param);
  },

  //设置设备围栏
  setFence:function (param){
    return BaseService.goEquipment("setFence", param);
  },

  //获取设备分类
  getCategoryTree(){
    return BaseService.go("ui/equipmentCategory/getTree",{});
  },

  //报修接待-新
  maintenanceReception(param){
    return BaseService.go("es/afterSaleExport/maintenanceReception", param);
  },

  //电子工牌查询
  findPersonnelAll(param){
    return BaseService.goCard("findPersonnelAll", param);
  },

  //电子工牌查询详情
  findPersonnel:function(data){
    return BaseService.goCard("findPersonnel", data);
  },

  //电子工牌保存
  electronCardSave:function(data){
    return BaseService.goCard("save", data);
  },

  //获取轨迹
  trajectory:function(data){
    return BaseService.goChart("trajectory", data);
  },

  //个人中心-每日工作量
  findCostData:function(data){
    let currentUser=app.globalData.userInfo;
    if(currentUser){
      data.departAuth=currentUser.dataAuthorityOfUser.depart;
    }
    return BaseService.go("chart/findCostData", data);
  },

  //个人中心-设备出勤率
  findAttendanceRate:function(data){
    let currentUser=app.globalData.userInfo;
    if(currentUser){
      data.departAuth=currentUser.dataAuthorityOfUser.depart;
    }
    //return BaseService.go("chart/findAttendanceRate", data);
    return BaseService.goChart("findAttendanceRate", data);
  },

  //人员轨迹
  trajectoryRecord:function(data){
    return BaseService.goCard("trajectoryRecord", data);
  },

  //考勤统计
  attendanceStatistics:function(data){
    return BaseService.goCard("attendanceStatistics", data);
  },

  //考勤统计-下级部门
  attendanceByDepart:function(data){
    return BaseService.goCard("attendanceStatisticsByDepart", data);
  },

  //员工考勤情况
  findOneClockIn:function(data){
    return BaseService.goCard("findOneClockIn", data);
  },

  //员工考勤非正常数据
  findAbnormalClockIn:function(data){
    return BaseService.goCard("findAbnormalClockIn", data);
  },

  //更新设备租赁时间
  updateLeaseTime:function(data){
    return BaseService.go("updateOrder/updateLeaseTime", data);
  },

  //出进围栏记录
  outInFenceCount:function(data){
    return BaseService.goCard("OutInFenceCount", data);
  },

  //出进围栏记录详情
  outInFenceRecord:function(data){
    return BaseService.goCard("OutInFenceRecord", data);
  },

  //个人中心平均时长
  avgRuntime:function(data){
    return BaseService.goDataReport("allEquipmentEachDayAvgRuntime", data);
  },

  //电子围栏同步es数据
  synchronFence:function(data){
    return BaseService.goCard("synchronFence", data);
  },

    //设备电子围栏同步es数据
    synchronEquipmentFence:function(data){
      return BaseService.goEquipment("equipmnetFence", data);
    },

  //考勤规则同步es数据
  synchronClockRule:function(data){
    return BaseService.goCard("synchronClockRule", data);
  },

  //配件采购厂家发货
  consignment:function(data){
    return BaseService.go("depot/consignment", data);
  },

  //配件入库
  inStorage:function(data){
    return BaseService.go("depot/inStorage", data);
  },

  //配件出库
  outStorage:function(data){
    return BaseService.go("depot/outStorage", data);
  },

  //建仓库
  getDepot:function(data){
    return BaseService.go("depot/getDepot", data);
  },

  //配件分类
  getPartsCategoryTree:function(data){
    return BaseService.go("ui/partsCategory/getTree", data);
  },

  //设备入库
  returnOrder:function(data){
    return BaseService.go("returnOrder/save", data);
  },

  //设备入库单作废
  deleteInStorage:function(data){
    return BaseService.go("returnOrder/deleteInStorage", data);
  },

  //设备保养/验收
  checkItem:function(data){
    return BaseService.go("returnOrder/checkItem", data);
  },

  //报修-2022-8新改
  addRepair:function(data){
    return BaseService.go("es/afterSaleRepair/repairs", data);
  },

  //维修转耗材
  changeToConsumable:function(data){
    return BaseService.go("es/afterSaleRepair/m2c", data);
  },

  //耗材转维修
  changeToMaintain:function(data){
    return BaseService.go("es/afterSaleRepair/c2m", data);
  },

  //维修闭单
  closeMaintainOrder:function(data){
    return BaseService.go("es/afterSaleRepair/closeMaintainOrder", data);
  },

  //删除维修工单
  delMaintainOrder:function(data){
    return BaseService.go("es/afterSaleRepair/delMaintainOrder", data);
  },

  //配件列表
  partList:function(data){
    return BaseService.go("depot/partList", data);
  },

  //配件采购单添加/修改
  purchaseSave:function(data){
    return BaseService.go("depot/purchaseSave", data);
  },

  //电子工牌步数统计
  stepRecord:function(data){
    return BaseService.goCard("stepRecord", data);
  },

  //手环有效工作时间和活跃度详情页面数据查询
  effectiveDetail:function(data){
    return BaseService.pythonEffectiveDetail("worktimePerson", data);
  },



  //更新t_purchase_equipment表
  updatePurchaseEquipment:function(data){
    return BaseService.go("updateOrder/updatePurchaseEquipment", data);
  },

  //公众号消息推送
  sendMessage:function(data){
    return BaseService.go("newsFeed/sendMessage", data);
  },

  //电子工牌上数间隔
  setUpperInterval:function(data){
    return BaseService.goCard("setUpperInterval", data);
  },

  // updatePurchaseEquipmentLogistic:function(data){
  //   return BaseService.go("updateOrder/updatePurchaseEquipmentLogistic", data);
  // },

  //保存考勤组
  saveAttendanceGroup:function(data){
    return BaseService.goCard("saveAttendanceGroup", data);
  },

  //查询工牌人员（按部门分组）
  getPersonnelGroupByDepart:function(){
    return BaseService.goCard("getPersonnelGroupByDepart",{});
  },

  //保存员工排班
  saveRostering:function(data){
    return BaseService.goCard("saveRostering",data);
  },

  //删除考勤组
  deleteAttendanceGroup:function(data){
    return BaseService.goCard("deleteAttendanceGroup",data);
  },

  //个人中心-人员出勤率
  personnelAttendanceRate:function(data){
    return BaseService.goCard("personnelAttendanceRate",data);
  },

  //活跃度统计-所有人员
  stepStatistics:function(data){
    return BaseService.goCard("stepStatistics",data);
  },

  //活跃度统计-单个人员按天
  stepStatisticsDay:function(data){
    return BaseService.goCard("stepStatisticsDay",data);
  },

  //保存设备后回调
  saveEquipmentTx:function (data){
    return BaseService.goDataOpr("saveEquipmentTx", data);
  },

  //获取考勤组和排班信息
  attendanceStatisticsByDay:function(data){
    return BaseService.goCard("attendanceStatisticsByDay",data);
  },

  //保存项目时提醒的相关信息更新到工牌表
  updateCardReceiver:function(data){
    return BaseService.goCard("updateCardReceiver",data);
  },

  //加班列表
  findWorkOverRecordList:function(data){
    return BaseService.goCard("findWorkOverRecordList",data);
  },

  //查询某个人员的排班和加班数据
  findWorkOverRecord:function(data){
    return BaseService.goCard("findWorkOverRecord",data);
  },

  //保存加班
  saveWorkOverTime:function(data){
    return BaseService.goCard("saveWorkOverTime",data);
  },

  //删除加班
  deleteWorkOverRecord:function(data){
    return BaseService.goCard("deleteWorkOverRecord",data);
  },

  //删除排班（用于修改人员项目的回调）
  deleteShift:function(data){
    return BaseService.goCard("deleteShift",data);
  },

  //设置设备围栏时查询可选人员
  findRecipient:function (param){
    return BaseService.goEquipment("findRecipient", param);
  },

  //设备电压变化
  findVol:function (param){
    return BaseService.syncPgSql("findVol", param);
  },

  //实时轨迹  
  findRunRecordToPgsql:function (param){
    return BaseService.syncPgSql("findRunRecordToPgsql", param);
  },

  //待岗记录
  jobWaitingCount:function(data){
    return BaseService.goCard("jobWaitingCount", data);
  },

  //待岗记录
  jobWaitingRecord:function(data){
    return BaseService.goCard("jobWaitingRecord", data);
  },

  //聚岗记录
  juGangRecord:function(data){
    return BaseService.goCard("juGangRecord", data);
  },

  //修改设备表，根据设备imei修改设备停机/开机状态，mongodb与es数据库一块同步
  updateEquipmentOffState:function (param){
    return BaseService.goEquipment("updateEquipmentOffState", param);
  },

  //修改redis开关机状态
  onAndOffState:function (param){
    return BaseService.goEquipment("onAndOffState", param);
  },

  //开关机查询指令日志
  findInstructLog:function (param){
    return BaseService.syncPgSql("findRun", param);
  },

  //指令-设置上数频率
  generalInstruction:function (data,port) {
    return BaseService.commonIot("instruct"+port+"/generalInstruction", data);
  },

  //个人中心设备运行时长图表（仅龙湖显示）
  findEquipmentGroupCategory:function(data){
    return BaseService.goChart("findEquipmentGroupCategory", data);
  },

  //设备类型出勤率图表（仅龙湖显示）
  findAttendanceRateGroupCategory:function(data){
    return BaseService.goChart("findAttendanceRateGroupCategory", data);
  },

  //获取耗材赠送历史记录
  freeConsumables(param){
    return BaseService.go("es/afterSaleRepair/freeConsumables", param);
  },

	//个人中心图表-设备类型出勤率、设备运行时长（新）
  findEquipmentGroupCategoryNew:function (data){
    return BaseService.goDataReport("findEquipmentGroupCategory", data);
	},
	
	//个人中心图表-设备出勤率（新）
	findEquipmentAllRate:function (data){
    return BaseService.goDataReport("findEquipmentAllRate", data);
	},
	
	//更换下来的旧模块归属仓库问题
  updateOldImeiState(param){
    return BaseService.go("imeiWarehouse/updateOldImeiState", param);
	},
	
	//日志
  operateLogSave(param){
    return BaseService.go("operateLog/save", param);
	},
	
	//添加报修后回调-是否修改设备状态为故障
	shutdown(data){
    return BaseService.go("es/afterSaleRepair/shutdown", data);
	},

	//建发推送-获取用户token
	jianfaToken(){
    return BaseService.go("jianfa/Token");
	},

	//建发推送
	jianfaSend(data){
    return BaseService.go("newsFeed/deviceError/JF",data);
	},

	//查模块列表
	findWarehouseImei(data){
    return BaseService.go("imeiWarehouse/findWareHouseImei",data);
	},

  //查询健康记录
	healthLog(data){
    return BaseService.health("health_log",data);
  },

  findClearPersonnelByWorkArea(data){
    return BaseService.goCard("findClearPersonnelByWorkArea", data);
  },
  //发送指令
  sendInstruction(data){
    return BaseService.sendInstruction("command", data);
  },

    //批量发送指令
    batchSendInstruction(data){
      return BaseService.sendInstruction("batch_command", data);
    },

    //发送tts语音
    sendMessageTTS(data){
      return BaseService.sendMessageTTS("command_sendMessage", data);
    },

    //保存任务
    saveTaskFunc(data){
      return BaseService.sendMessageTTS("command_sendMessage", data);
    },
    //同步排班表p_employee_scheduling_area
    syncEmployeeSchedulingArea(data){
      return BaseService.goCard("syncEmployeeSchedulingArea", data);
    },

    //查询任务列表
    findTaskList(data){
      console.log();
      return BaseService.task("findTaskList", data);
    },

    //任务清单生成审批流程
    createApprover(data){
      return BaseService.task("createApprover", data);
    },
    //集体任务创建后同步任务清单
    collectiveTaskToTaskList(data){
      return BaseService.task("collectiveTaskToTaskList", data);
    },
    //集体任务创建后同步任务清单
    updateCollectiveTaskToTaskList(data){
      return BaseService.task("updateCollectiveTaskToTaskList", data);
    },

    //创建的计划，手动生成清单
    createTaskListHand(data){
      return BaseService.task("createTaskList", data);
    },

    //修改了巡检任务后同步计划
    syncTaskPlan(data){
      return BaseService.task("syncTaskPlan", data);
    },

    //删除任务后同步计划
    deleteTaskSyncTaskPlan(data){
      return BaseService.task("deleteTaskSyncTaskPlan", data);
    },

    //查询任务清单没有完成的订单/物业工单没有完成的订单
    getUnFinishTaskAndCleanOrderFunc(data){
      return BaseService.task("unFinishTaskAndCleanOrder", data);
    },

    //查询并创建仓库信息
    findStorage(data){
      return BaseService.storage("findStorage", data);
    },
      //设置设备围栏
  doTransfer:function (param){
    return BaseService.goEquipment("equipmentTransfer", param);
  },

  doStoreIn:function (param){
    return BaseService.goEquipment("equipmentStoreIn", param);
  },
  doStoreOut:function (param){
    return BaseService.goEquipment("equipmentStoreOut", param);
  },
  //设备报废
  goScrap:function (param){
    return BaseService.goEquipment("equipmentScrap", param);
  },
}
